//1s计时器
bool screen_after_1s(){
  if(millis()-system_millis_last_screen_1>=1000){
    system_millis_last_screen_1 = millis();
    return true;
  }else{
    return false;
  }
}
//5s计时器
bool screen_after_5s(){
  if(millis()-system_millis_last_screen_5>=5000){
    system_millis_last_screen_5 = millis();
    return true;
  }else{
    return false;
  }
}

//屏幕初始化，开机执行
void SCREEN_init(){
  SCREEN.init();
  SCREEN.setRotation(1);   // 0为旋转0度，1旋转90度，2旋转180度，3旋转270度，方向为顺时针
  SCREEN.fillScreen(TFT_BLACK);
  SCREEN.loadFont(SHanSans_14);    //选择字体库
  
  screen_page_0_init();
  ec11.Operated_Listener(screen_backlight_timer);
}
// 屏幕主循环
void SCREEN_loop(){
  controll_screen_backlight();
  switch(screen_page_now){
    case 0:
      screen_page_0_loop();
      break;
    case 100:
      screen_page_100_loop();
      break;
    case 200:
      screen_page_200_loop();
    break;
  }
}
//背光控制计时触发
void screen_backlight_timer(){
  system_millis_backlight = millis();
  if(digitalRead(ESP_PIN_SCREEN_BL)==LOW){
    digitalWrite(ESP_PIN_SCREEN_BL, HIGH);
    ec11.eventEnable(true);
  }
}
//背光控制
void controll_screen_backlight(){
  if(system_lcd_backlight_time == 0          //常开
    || digitalRead(ESP_PIN_SCREEN_BL) == LOW){   
    return;
  }
  unsigned long backlight_opentime = millis() - system_millis_backlight;
  switch(system_lcd_backlight_time){
    case 1:       //3分
      if(backlight_opentime>180000){
        screen_page_now = 0;
        screen_page_0_init();
        digitalWrite(ESP_PIN_SCREEN_BL, LOW);
        ec11.eventEnable(false);    //关闭按钮响应
      }
      break;
    case 2:       //5分
      if(backlight_opentime>300000){
        screen_page_now = 0;
        screen_page_0_init();
        digitalWrite(ESP_PIN_SCREEN_BL, LOW);
        ec11.eventEnable(false);    //关闭按钮响应
      }
      break;
    case 3:       //10分
      if(backlight_opentime>600000){
        screen_page_now = 0;
        screen_page_0_init();
        digitalWrite(ESP_PIN_SCREEN_BL, LOW);
        ec11.eventEnable(false);    //关闭按钮响应
      }
      break;
    case 5:       //15分
      if(backlight_opentime>900000){
        screen_page_now = 0;
        screen_page_0_init();
        digitalWrite(ESP_PIN_SCREEN_BL, LOW);
        ec11.eventEnable(false);    //关闭按钮响应
      }
      break;
    case 6:       //30分
      if(backlight_opentime>1800000){
        screen_page_now = 0;
        screen_page_0_init();
        digitalWrite(ESP_PIN_SCREEN_BL, LOW);
        ec11.eventEnable(false);    //关闭按钮响应
      }
      break;
  } 
}
//主界面左右切换
void screen_page_CW_home(){
  switch(screen_page_now){
    case 0:
      screen_page_now = 100;
      screen_page_100_init();
      break;
    case 100:
      screen_page_now = 200;
      screen_page_200_init();
      break;
    case 200:
      screen_page_now = 0;
      screen_page_0_init();
    break;
  }
}
void screen_page_CCW_home(){
  switch(screen_page_now){
    case 0:
      screen_page_now = 200;
      screen_page_200_init();
      break;
    case 100:
      screen_page_now = 0;
      screen_page_0_init();
      break;
    case 200:
      screen_page_now = 100;
      screen_page_100_init();
    break;
  }
}

//主页0 显示常用信息
void screen_page_0_init(){
  SCREEN.fillScreen(TFT_BLACK);
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(F("V"), 4, 81);
  SCREEN.drawString(F("A"), 4, 97);
  SCREEN.drawString(F("W"), 2, 113);
  //电池图标
  SCREEN.fillRect(78, 24, 6, 4, TFT_BROWN);
  SCREEN.fillRect(68, 28, 25, 46, TFT_BROWN);
  //输出图标
  SCREEN.drawBitmap(109, 24, img_light,50, 50, TFT_DARKGREY);
  //太阳图标
  SCREEN.drawBitmap(1, 24, img_sun,50, 50, TFT_DARKGREY);
  // 重置按下事件
  ec11.add_Key_Listener(NULL,NULL,screen_page_0_longpress);
  //左右换页事件
  ec11.addKnobListener(screen_page_CW_home, screen_page_CCW_home);
  //首次刷新显示内容
  screen_page_0_refresh_1s();
  screen_page_0_refresh_5s();
}
void screen_page_0_loop(){
  if(screen_after_1s()){
    screen_page_0_refresh_1s();
  }
  if(screen_after_5s()){
    screen_page_0_refresh_5s();
  }
}
void screen_page_0_refresh_1s(){
  //太阳能图标
  if(system_mppt_efficiency>0){
    SCREEN.drawBitmap(1, 24, img_sun,50, 50, TFT_ORANGE);
    SCREEN.setTextColor(TFT_RED, TFT_ORANGE);
    SCREEN.drawNumber(system_mppt_efficiency, 18, 42);
  }else{
    SCREEN.drawBitmap(1, 24, img_sun,50, 50, TFT_DARKGREY);
  }
  //输出图标
  system_output_state==0? SCREEN.drawBitmap(109, 24, img_light,50, 50, TFT_MAGENTA) : SCREEN.drawBitmap(109, 24, img_light,50, 50, TFT_DARKGREY);
  
  //显示电压电流功率
  SCREEN.fillRect(16, 81, 145, 48, TFT_BLACK);
  int decimal = 2;
  //输入
  SCREEN.setTextColor(TFT_YELLOW, TFT_BLACK);
  SCREEN.drawFloat(system_ina_voltage_1, 2, 16, 81);
  SCREEN.drawFloat(system_ina_current_1, 2, 16, 97);
  system_ina_power_1>99.99? decimal = 1 : decimal = 2;
  SCREEN.drawFloat(system_ina_power_1,   decimal, 16, 113);
  //电池
  SCREEN.setTextColor(TFT_DARKGREEN, TFT_BLACK);
  SCREEN.drawFloat(system_ina_voltage_2, 2, 70, 81);
  SCREEN.drawFloat(system_ina_current_2, 2, 70, 97);
  system_ina_power_2>99.99? decimal = 1 : decimal = 2;
  SCREEN.drawFloat(system_ina_power_2,   decimal, 70, 113);
  //输出
  SCREEN.setTextColor(TFT_ORANGE, TFT_BLACK);
  SCREEN.drawFloat(system_ina_voltage_3, 2, 125, 81);
  SCREEN.drawFloat(system_ina_current_3, 2, 125, 97);
  system_ina_power_3>99.99? decimal = 1 : decimal = 2;
  SCREEN.drawFloat(system_ina_power_3,   decimal, 125, 113);
}
void screen_page_0_refresh_5s(){
  //运行时间
  SCREEN.fillRect(0, 0, 89, 16, TFT_BLACK);
  STime timenow;
  String ddhhmm = timenow.getD_str() +":"+ timenow.getH_str() +":"+ timenow.getM_str();
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(ddhhmm, 2, 1);
  //wifi信号
  if(system_internet_mode != 0){
    SCREEN.fillTriangle(139, 15, 158, 15, 158, 1, TFT_WHITE);
    int wifi_w = 19 * constrain(2 * (WiFi.RSSI() + 100), 0, 100) / 100;
    int wifi_h = 15 * constrain(2 * (WiFi.RSSI() + 100), 0, 100) / 100;
    SCREEN.fillTriangle(139, 15, 139+wifi_w, 15, 139+wifi_w, 15-wifi_h, TFT_DARKGREEN);
  }else{
    SCREEN.fillTriangle(139, 15, 158, 15, 158, 1, TFT_DARKGREY);
  }
  //温度
  SCREEN.fillRect(90, 0, 48, 16, TFT_BLACK);
  SCREEN.drawString(String(system_temperature_ntc)+"℃", 90, 1);
  //电池电量
  SCREEN.fillRect(68, 28, 25, 46, TFT_BROWN);
  int bat_h = 46 * system_battery_percent/100;
  SCREEN.fillRect(68, 74-bat_h, 25, bat_h, TFT_DARKGREEN);
  SCREEN.setTextColor(TFT_WHITE);
  if(system_battery_percent<100){
    SCREEN.drawNumber(system_battery_percent, 72, 42);
  }else{
    SCREEN.drawNumber(system_battery_percent, 69, 42);
  }
}
//输出模式为普通模式时，主界面可长按实现输出开关
void screen_page_0_longpress(){
  if(system_output_control_mode == 0){
    system_output_state == 0? system_output_state = 1 : system_output_state = 0;
  }
}
//主页1 设置界面入口
void screen_page_100_init(){
  //显示内容初始化
  SCREEN.fillScreen(TFT_BLACK);
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(F("1 输出控制"), 20, 0);
  SCREEN.drawString(F("2 电池参数"), 20, 17);
  SCREEN.drawString(F("3 工作模式"), 20, 33);
  SCREEN.drawString(F("4 温控设置"), 20,49);
  SCREEN.drawString(F("5 屏幕背光:"), 20, 65);
  switch(system_lcd_backlight_time){
      case 0:
        SCREEN.drawString(F("常开"), 95, 65);
        break;
      case 1:
        SCREEN.drawString(F("3分钟"), 95, 65);
        break;
      case 2:
        SCREEN.drawString(F("5分钟"), 95, 65);
        break;
      case 3:
        SCREEN.drawString(F("10分钟"), 95, 65);
        break;
      case 4:
        SCREEN.drawString(F("15分钟"), 95, 65);
        break;
      case 5:
        SCREEN.drawString(F("30分钟"), 95, 65);
        break;
  }
  SCREEN.drawString(F("6 恢复出厂设置"), 20, 81);
  SCREEN.drawString(F("7 系统信息"), 20, 97);
  SCREEN.drawString(F("8 返回"), 20, 113);
  //按下进入选择，长按返回
  ec11.add_Key_Listener(NULL, screen_page_100_pressedEvent, screen_page_100_longPressedEvent);
  //初始化选择项
  screen_page_100_selected = 0;
}
void screen_page_100_loop(){
  if(screen_page_100_selected!=0){
    SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
    SCREEN.drawString(">", 2, (screen_page_100_selected -1) * 16 + 1);
    SCREEN.drawString("<", 151, (screen_page_100_selected -1) * 16 + 1);
  }
}
void screen_page_100_pressedEvent(){
  //具体功能设置页
  switch(screen_page_100_selected){
    case 0:    //未选择
      ec11.addKnobListener(screen_page_100_CW, screen_page_100_CCW);
      screen_page_100_selected = 1;
      break;
    case 1:    //输出控制
      screen_page_110_init();
      break;
    case 2:    //电池参数
      screen_page_120_init();
      break;
    case 3:    //工作模式
      screen_page_130_init();
      break;
    case 4:    //保护温度设置
      screen_page_140_init();
      break;
    case 5:    //屏幕背光
      system_lcd_backlight_time ++;
      if(system_lcd_backlight_time>5){system_lcd_backlight_time = 0;}
      SCREEN.fillRect(95, 65, 56, 14, TFT_BLACK);
      SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
      switch(system_lcd_backlight_time){
        case 0:
          SCREEN.drawString(F("常开"), 95, 65);
          break;
        case 1:
          SCREEN.drawString(F("3分钟"), 95, 65);
          break;
        case 2:
          SCREEN.drawString(F("5分钟"), 95, 65);
          break;
        case 3:
          SCREEN.drawString(F("10分钟"), 95, 65);
          break;
        case 4:
          SCREEN.drawString(F("15分钟"), 95, 65);
          break;
        case 5:
          SCREEN.drawString(F("30分钟"), 95, 65);
          break;
      }
      break;
    case 6:    //恢复出厂设置
      SCREEN.fillRect(120, 81, 30, 14, TFT_BLACK);
      SCREEN.setTextColor(TFT_RED, TFT_BLACK);
      SCREEN.drawNumber(system_restore_factory_settings - sys_RFS_count, 120, 81);
      if(system_restore_factory_settings - sys_RFS_count == 0 ){
        FactoryReset();
        ESP.restart();
      }
      sys_RFS_count ++;
      break;
    case 7:    //系统信息
      screen_page_170_init();
      break;
    case 8:    //返回
      screen_page_100_longPressedEvent();
      break;
  }
}

void screen_page_100_longPressedEvent(){
  SaveSettings();    //保存设置参数
  screen_page_100_selected = 0;
  ec11.addKnobListener(screen_page_CW_home, screen_page_CCW_home);
  screen_page_100_init();
}
void screen_page_100_CW(){
  if(screen_page_100_selected == 6){ SCREEN.fillRect(120, 81, 30, 14, TFT_BLACK); sys_RFS_count = 0;}  //仅用于恢复出厂设置
  screen_page_1x0_knob(Knob_CW, 1, 8);
}
void screen_page_100_CCW(){
  if(screen_page_100_selected == 6){ SCREEN.fillRect(120, 81, 30, 14, TFT_BLACK);  sys_RFS_count = 0;}  //仅用于恢复出厂设置
   screen_page_1x0_knob(Knob_CCW, 1, 8);
}
//1xx通用的长按方法和旋转编码方法
void screen_page_1x0_longPressedEvent(uint8_t sel){
  screen_page_100_init();
  screen_page_100_selected = sel;
  ec11.addKnobListener(screen_page_100_CW, screen_page_100_CCW);
}
void screen_page_1x0_knob(uint8_t cxw, uint8_t selmin, uint8_t selmax){
  if(cxw==Knob_CW){
    screen_page_100_selected++;
    if(screen_page_100_selected>selmax){
      screen_page_100_selected = selmin;
    }
  }else if(cxw==Knob_CCW){
    screen_page_100_selected--;
    if(screen_page_100_selected<selmin){
      screen_page_100_selected = selmax;
    }
  }
  SCREEN.fillRect(0,0,10,128,TFT_BLACK);
  SCREEN.fillRect(151,0,10,128,TFT_BLACK);
}

//输出控制 110
void screen_page_110_init(){
  SCREEN.fillScreen(TFT_BLACK); 
  SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
  SCREEN.drawString(F("1 输出控制"), 20, 0);
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(F("电流限制:"), 20, 17);
  SCREEN.drawFloat(output_current_limit, 1, 95, 17);
  SCREEN.drawString(F("A"), 135, 17);
  SCREEN.drawString(F("控制模式:"), 20, 33);
  switch(system_output_control_mode){
    case 0:
      SCREEN.drawString(F("普通模式"), 90, 33);
      break;
    case 1:
      SCREEN.drawString(F("光控模式"), 90, 33);
      break;
    case 2:
      SCREEN.drawString(F("光控+时控"), 85, 33);
      break;
  }
  SCREEN.drawString(F("夜间打开:"), 20,49);
  SCREEN.drawNumber(system_output_optical_control_time, 95, 49);
  SCREEN.drawString(F("H"), 135, 49);
  SCREEN.drawString(F("当前状态:"), 20, 65);
  switch(system_output_state){
    case 0:
      SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
      SCREEN.drawString(F("开"), 100, 65);
      break;
    case 1:
      SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
      SCREEN.drawString(F("关"), 100, 65);
      break;
  }
  SCREEN.drawString(F("返回"), 20, 81);
  screen_page_100_selected = 2;
  ec11.addKnobListener(screen_page_110_CW, screen_page_110_CCW);
  ec11.add_Key_Listener(NULL, screen_page_110_pressedEvent, screen_page_110_longPressedEvent);
}
void screen_page_110_loop(){
  SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
  SCREEN.drawString(F(">"), 2, screen_page_100_selected * 16 + 1);
  SCREEN.drawString(F("<"), 151, screen_page_100_selected * 16 + 1);
}
void screen_page_110_pressedEvent(){
  switch(screen_page_100_selected){
    case 2:      //电流限制
      if(ec11.CW_Event == screen_page_112_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawFloat(output_current_limit, 1, 95, 17);
        ec11.addKnobListener(screen_page_110_CW, screen_page_110_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawFloat(output_current_limit, 1, 95, 17);
        ec11.addKnobListener(screen_page_112_CW, screen_page_112_CCW);
      }
      break;
    case 3:      //控制模式
      system_output_control_mode ++;
      if(system_output_control_mode>2){system_output_control_mode = 0;}
      SCREEN.fillRect(85, 33, 65, 14, TFT_BLACK);
      switch(system_output_control_mode){
        case 0:
          SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
          SCREEN.drawString(F("普通模式"), 90, 33);
          break;
        case 1:
          SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
          SCREEN.drawString(F("光控模式"), 90, 33);
          break;
        case 2:
          SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
          SCREEN.drawString(F("光控+时控"), 85, 33);
          break;
      }
      break;
    case 4:      //夜间打开
      if(ec11.CW_Event == screen_page_114_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawNumber(system_output_optical_control_time, 95, 49);
        ec11.addKnobListener(screen_page_110_CW, screen_page_110_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawNumber(system_output_optical_control_time, 95, 49);
        ec11.addKnobListener(screen_page_114_CW, screen_page_114_CCW);
      }
      break;
    case 5:      //当前状态
      system_output_state==0? system_output_state=1 : system_output_state=0;
      SCREEN.fillRect(100, 65, 14, 14, TFT_BLACK);
      switch(system_output_state){
        case 0:
          SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
          SCREEN.drawString(F("开"), 100, 65);
          break;
        case 1:
          SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
          SCREEN.drawString(F("关"), 100, 65);
          break;
      }
      break;
    case 6:      //返回
      screen_page_1x0_longPressedEvent(1);
      break;
  }
}
void screen_page_110_longPressedEvent(){
  screen_page_1x0_longPressedEvent(1);
}
void screen_page_110_CW(){
  screen_page_1x0_knob(Knob_CW, 2, 6);
}
void screen_page_110_CCW(){
  screen_page_1x0_knob(Knob_CCW, 2, 6);
}
//调节电流
void screen_page_112_CW(){ 
  output_current_limit += 0.1;
  if(output_current_limit > PCBA_CURRENT_LIMIT){ output_current_limit = PCBA_CURRENT_LIMIT;} 
  SCREEN.fillRect(95, 17, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(output_current_limit, 1, 95, 17);
}
void screen_page_112_CCW(){ 
  output_current_limit -= 0.1;
  if(output_current_limit < 0.1){output_current_limit = 0.1;}
  SCREEN.fillRect(95, 17, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(output_current_limit, 1, 95, 17);
}
//调节夜间打开时长
void screen_page_114_CW(){ 
  system_output_optical_control_time += 1;
  if(system_output_optical_control_time > 12){ system_output_optical_control_time = 12;} 
  SCREEN.fillRect(95, 49, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawNumber(system_output_optical_control_time, 95, 49);
}
void screen_page_114_CCW(){ 
  system_output_optical_control_time -= 1;
  if(system_output_optical_control_time < 1){system_output_optical_control_time = 1;}
  SCREEN.fillRect(95, 49, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawNumber(system_output_optical_control_time, 95, 49);
}
//电池参数设置
void screen_page_120_init(){
  SCREEN.fillScreen(TFT_BLACK); 
  SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
  SCREEN.drawString(F("2 电池参数"), 20, 0);
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(F("充电电压:"), 20, 17);
  SCREEN.drawFloat(system_bat_maxv, 1, 95, 17);
  SCREEN.drawString(F("V"), 135, 17);
  SCREEN.drawString(F("满电电压:"), 20, 33);
  SCREEN.drawFloat(system_bat_fullv, 1, 95, 33);
  SCREEN.drawString(F("V"), 135, 33);
  SCREEN.drawString(F("最小电压:"), 20, 49);
  SCREEN.drawFloat(system_bat_minv, 1, 95, 49);
  SCREEN.drawString(F("V"), 135, 49);
  SCREEN.drawString(F("输出保护:"), 20, 65);
  SCREEN.drawFloat(system_bat_protectv, 1, 95, 65);
  SCREEN.drawString(F("V"), 135, 65);
  SCREEN.drawString(F("电流限制:"), 20, 81);
  SCREEN.drawFloat(system_bat_current_limit, 1, 95, 81);
  SCREEN.drawString(F("A"), 135, 81);
  SCREEN.drawString(F("返回"), 20, 97);
  screen_page_100_selected = 2;
  ec11.add_Key_Listener(NULL, screen_page_120_pressedEvent, screen_page_120_longPressedEvent);
  ec11.addKnobListener(screen_page_120_CW, screen_page_120_CCW);
}
void screen_page_120_pressedEvent(){
  switch(screen_page_100_selected){
    case 2:
      if(ec11.CW_Event == screen_page_122_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawFloat(system_bat_maxv, 1, 95, 17);
        ec11.addKnobListener(screen_page_120_CW, screen_page_120_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawFloat(system_bat_maxv, 1, 95, 17);
        ec11.addKnobListener(screen_page_122_CW, screen_page_122_CCW);
      }
      break;
    case 3:
      if(ec11.CW_Event == screen_page_123_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawFloat(system_bat_fullv, 1, 95, 33);
        ec11.addKnobListener(screen_page_120_CW, screen_page_120_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawFloat(system_bat_fullv, 1, 95, 33);
        ec11.addKnobListener(screen_page_123_CW, screen_page_123_CCW);
      }
      break;
    case 4:
      if(ec11.CW_Event == screen_page_124_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawFloat(system_bat_minv, 1, 95, 49);
        ec11.addKnobListener(screen_page_120_CW, screen_page_120_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawFloat(system_bat_minv, 1, 95, 49);
        ec11.addKnobListener(screen_page_124_CW, screen_page_124_CCW);
      }
      break;
    case 5:
      if(ec11.CW_Event == screen_page_125_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawFloat(system_bat_protectv, 1, 95, 65);
        ec11.addKnobListener(screen_page_120_CW, screen_page_120_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawFloat(system_bat_protectv, 1, 95, 65);
        ec11.addKnobListener(screen_page_125_CW, screen_page_125_CCW);
      }
      break;
    case 6:
      if(ec11.CW_Event == screen_page_126_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawFloat(system_bat_current_limit, 1, 95, 81);
        ec11.addKnobListener(screen_page_120_CW, screen_page_120_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawFloat(system_bat_current_limit, 1, 95, 81);
        ec11.addKnobListener(screen_page_126_CW, screen_page_126_CCW);
      }
      break;
    case 7:
      screen_page_120_longPressedEvent();
      break;
  }
}
void screen_page_120_longPressedEvent(){
  screen_page_1x0_longPressedEvent(2);
}
void screen_page_120_CW(){
  screen_page_1x0_knob(Knob_CW, 2, 7);
}
void screen_page_120_CCW(){
  screen_page_1x0_knob(Knob_CCW, 2, 7);
}
// 参数设定旋钮
void screen_page_122_CW(){
  system_bat_maxv += 0.1;
  if(system_bat_maxv > PCBA_VOLTAGE_LIMIT){ system_bat_maxv = PCBA_VOLTAGE_LIMIT;} 
  SCREEN.fillRect(95, 17, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_maxv, 1, 95, 17);
}
void screen_page_122_CCW(){
  system_bat_maxv -= 0.1;
  if(system_bat_maxv < 0){system_bat_maxv = 0;}
  SCREEN.fillRect(95, 17, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_maxv, 1, 95, 17);
}
void screen_page_123_CW(){
  system_bat_fullv += 0.1;
  if(system_bat_fullv > system_bat_maxv){ system_bat_fullv = system_bat_maxv;} 
  SCREEN.fillRect(95, 33, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_fullv, 1, 95, 33);
}
void screen_page_123_CCW(){
  system_bat_fullv -= 0.1;
  if(system_bat_fullv < system_bat_minv){system_bat_fullv = system_bat_minv;}
  SCREEN.fillRect(95, 33, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_fullv, 1, 95, 33);
}
void screen_page_124_CW(){
  system_bat_minv += 0.1;
  if(system_bat_minv > system_bat_maxv){ system_bat_minv = system_bat_maxv;} 
  SCREEN.fillRect(95, 49, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_minv, 1, 95, 49);
}
void screen_page_124_CCW(){
  system_bat_minv -= 0.1;
  if(system_bat_minv < 0){system_bat_minv = 0;}
  SCREEN.fillRect(95, 49, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_minv, 1, 95, 49);
}
void screen_page_125_CW(){
  system_bat_protectv += 0.1;
  if(system_bat_protectv > system_bat_maxv){ system_bat_protectv = system_bat_maxv;} 
  SCREEN.fillRect(95, 65, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_protectv, 1, 95, 65);
}
void screen_page_125_CCW(){
  system_bat_protectv -= 0.1;
  if(system_bat_protectv < 0){system_bat_protectv = 0;}
  SCREEN.fillRect(95, 65, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_protectv, 1, 95, 65);
}
void screen_page_126_CW(){
  system_bat_current_limit += 0.1;
  if(system_bat_current_limit > PCBA_CURRENT_LIMIT){ system_bat_current_limit = PCBA_CURRENT_LIMIT;} 
  SCREEN.fillRect(95, 81, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_current_limit, 1, 95, 81);
}
void screen_page_126_CCW(){
  system_bat_current_limit -= 0.1;
  if(system_bat_current_limit < 0){system_bat_current_limit = 0;}
  SCREEN.fillRect(95, 81, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawFloat(system_bat_current_limit, 1, 95, 81);
}
//工作模式
void screen_page_130_init(){
  SCREEN.fillScreen(TFT_BLACK); 
  SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
  SCREEN.drawString(F("3 工作模式"), 20, 0);
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(F("离线运行"), 20, 17);
  SCREEN.drawString(F("MQTT"), 20, 33);
  SCREEN.drawString(F("Blinker"), 20, 49);
  SCREEN.fillTriangle(100,system_internet_mode*16+23,112,system_internet_mode*16+18,112,system_internet_mode*16+29,TFT_DARKGREEN);
  SCREEN.drawString(F("串口输出状态:"), 20, 65);
  if(system_uart_print_state == 0){
    SCREEN.drawString(F("关"), 120, 65);
  }else{
    SCREEN.drawString(F("开"), 120, 65);
  }
  SCREEN.drawString(F("重新配网"), 20, 81);
  SCREEN.drawString(F("返回"), 20, 97);
  screen_page_100_selected = 2;
  ec11.add_Key_Listener(NULL, screen_page_130_pressedEvent, screen_page_130_longPressedEvent);
  ec11.addKnobListener(screen_page_130_CW, screen_page_130_CCW);
}
void screen_page_130_pressedEvent(){
  switch(screen_page_100_selected){
    case 2:  //离线模式
      system_internet_mode = 0;
      WiFi.disconnect(true, false);
      break;
    case 3:  //MQTT模式
      system_internet_mode = 1;
      internet_setup();
      break;
    case 4:   //blinker模式
      system_internet_mode = 2;
      internet_setup();
      break;
    case 5:   //串口输出状态开关
      if(system_uart_print_state == 0){
        system_uart_print_state = 1;
        SCREEN.fillRect(120, 65, 14, 14, TFT_BLACK);
         SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawString(F("开"), 120, 65);
      }else{
        system_uart_print_state = 0;
        SCREEN.fillRect(120, 65, 14, 14, TFT_BLACK);
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawString(F("关"), 120, 65);
      }
      break;
    case 6:
      //配网，退出配网时需要重置wifi
      WiFi.disconnect(true, false);
      if(ec11.CW_Event == screen_page_130_CW){
        system_set_internet = 1;
        ec11.addKnobListener(NULL, NULL);
        SCREEN.fillRect(20, 81, 60, 14, TFT_BLACK);
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawString(F("重新配网"), 20, 81);
      }else{
        system_set_internet = 0;
        internet_setup();
        ec11.addKnobListener(screen_page_130_CW, screen_page_130_CCW);
        SCREEN.fillRect(20, 81, 60, 14, TFT_BLACK);
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawString(F("重新配网"), 20, 81);
      }
      break;
  }
  SCREEN.fillRect(100, 17, 14, 48, TFT_BLACK);
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.fillTriangle(100,system_internet_mode*16+23,112,system_internet_mode*16+18,112,system_internet_mode*16+29,TFT_DARKGREEN);
  //放在最后，否则不会消除三角形选择标识
  if(screen_page_100_selected ==7 ){
      screen_page_130_longPressedEvent();
  }
}
void screen_page_130_longPressedEvent(){
  screen_page_1x0_longPressedEvent(3);
}
void screen_page_130_CW(){
  screen_page_1x0_knob(Knob_CW, 2, 7); 
}
void screen_page_130_CCW(){
  screen_page_1x0_knob(Knob_CCW, 2, 7);
}

//温度保护
void screen_page_140_init(){
  SCREEN.fillScreen(TFT_BLACK); 
  SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
  SCREEN.drawString(F("4 温控设置"), 20, 0);
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(F("风扇启动:"), 20, 17);
  SCREEN.drawNumber(system_fan_fanOpen_TEM, 95, 17);
  SCREEN.drawString(F("℃"), 135, 17);
  SCREEN.drawString(F("过热保护:"), 20, 33);
  SCREEN.drawNumber(system_protect_TEM, 95, 33);
  SCREEN.drawString(F("℃"), 135, 33);
  SCREEN.drawString(F("返回"), 20, 49);
  screen_page_100_selected = 2;
  ec11.add_Key_Listener(NULL, screen_page_140_pressedEvent, screen_page_140_longPressedEvent);
  ec11.addKnobListener(screen_page_140_CW, screen_page_140_CCW);
}
void screen_page_140_pressedEvent(){
  switch(screen_page_100_selected){
    case 2:
      if(ec11.CW_Event == screen_page_142_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawNumber(system_fan_fanOpen_TEM, 95, 17);
        ec11.addKnobListener(screen_page_140_CW, screen_page_140_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawNumber(system_fan_fanOpen_TEM, 95, 17);
        ec11.addKnobListener(screen_page_142_CW, screen_page_142_CCW);
      }
      break;
    case 3:
      if(ec11.CW_Event == screen_page_143_CW){
        SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
        SCREEN.drawNumber(system_protect_TEM, 95, 33);
        ec11.addKnobListener(screen_page_140_CW, screen_page_140_CCW);
      }else{
        SCREEN.setTextColor(TFT_RED, TFT_BLACK);
        SCREEN.drawNumber(system_protect_TEM, 95, 33);
        ec11.addKnobListener(screen_page_143_CW, screen_page_143_CCW);
      }
      break;
    case 4:
      screen_page_140_longPressedEvent();
      break;
  }
}
void screen_page_140_longPressedEvent(){
  screen_page_1x0_longPressedEvent(4);
}
void screen_page_140_CW(){
  screen_page_1x0_knob(Knob_CW, 2, 4);
}
void screen_page_140_CCW(){
  screen_page_1x0_knob(Knob_CCW, 2, 4);
}

// 参数设定旋钮
void screen_page_142_CW(){
  system_fan_fanOpen_TEM += 1;
  if(system_fan_fanOpen_TEM > system_protect_TEM){ system_fan_fanOpen_TEM = system_protect_TEM;} 
  SCREEN.fillRect(95, 17, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawNumber(system_fan_fanOpen_TEM, 95, 17);
}
void screen_page_142_CCW(){
  system_fan_fanOpen_TEM -= 1;
  if(system_fan_fanOpen_TEM < 0){system_fan_fanOpen_TEM = 0;}
  SCREEN.fillRect(95, 17, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawNumber(system_fan_fanOpen_TEM, 95, 17);
}
void screen_page_143_CW(){
  system_protect_TEM += 1;
  if(system_protect_TEM > 90){ system_protect_TEM = 90;} 
  SCREEN.fillRect(95, 33, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawNumber(system_protect_TEM, 95, 33);
}
void screen_page_143_CCW(){
  system_protect_TEM -= 1;
  if(system_protect_TEM < 30){system_protect_TEM = 30;}
  SCREEN.fillRect(95, 33, 40, 14, TFT_BLACK);
  SCREEN.setTextColor(TFT_RED, TFT_BLACK);
  SCREEN.drawNumber(system_protect_TEM, 95, 33);
}
//系统信息
void screen_page_170_init(){
  SCREEN.fillScreen(TFT_BLACK); 
  SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
  SCREEN.drawString(F("7 系统信息"), 20, 0);
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(F("固件版本:"), 20, 17);
  SCREEN.drawString(sys_version, 85, 17);
  SCREEN.drawString(F("MAC地址:"), 20, 33);
  SCREEN.drawString(sys_mac_address, 20, 49);
  SCREEN.drawString(F("IP地址:"), 20, 65);
  if(system_internet_mode!=0){
    sys_ip_address = WiFi.localIP().toString();
  }
  SCREEN.drawString(sys_ip_address, 20, 81);
  SCREEN.drawString(F("OTA升级:"), 20, 97);
  system_enable_OTA == 1? SCREEN.drawString(F("开"), 100, 97) :  SCREEN.drawString(F("关"), 100, 97);
  SCREEN.drawString(F("返回"), 20, 113);
  screen_page_100_selected = 7;
  ec11.add_Key_Listener(NULL, screen_page_170_pressedEvent, screen_page_170_longPressedEvent);
  ec11.addKnobListener(screen_page_170_CCCW, screen_page_170_CCCW);
}

void screen_page_170_pressedEvent(){
  if(screen_page_100_selected==7){
    system_enable_OTA == 1? system_enable_OTA = 0 : system_enable_OTA = 1;
    SCREEN.fillRect(100, 97, 14, 14, TFT_BLACK);
    SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
    system_enable_OTA == 1? SCREEN.drawString(F("开"), 100, 97) :  SCREEN.drawString(F("关"), 100, 97);
  }else if(screen_page_100_selected == 8){
    screen_page_170_longPressedEvent();
  }
}
void screen_page_170_longPressedEvent(){
  screen_page_1x0_longPressedEvent(7);
}
void screen_page_170_CCCW(){
  screen_page_100_selected == 7? screen_page_100_selected = 8 : screen_page_100_selected = 7;
  SCREEN.fillRect(0,0,10,128,TFT_BLACK);
  SCREEN.fillRect(151,0,10,128,TFT_BLACK);
}

//主页2 运行状态
void screen_page_200_init(){
  SCREEN.fillScreen(TFT_BLACK); 
  SCREEN.setTextColor(TFT_WHITE, TFT_BLACK);
  SCREEN.drawString(F("MPPT控制器"), 28, 3);
  SCREEN.setTextColor(TFT_GOLD, TFT_BLACK);
  SCREEN.drawString(F("输入:"), 10, 25);
  SCREEN.drawFloat(store_in_kwh, 2, 52,26);
  SCREEN.drawString(F("KWh"), 120, 25);
  SCREEN.setTextColor(TFT_DARKGREEN, TFT_BLACK);
  SCREEN.drawString(F("电池:"), 10, 41);
  SCREEN.drawFloat(store_bat_kwh, 2, 52,42);
  SCREEN.drawString(F("KWh"), 120, 41);
  SCREEN.setTextColor(TFT_BROWN, TFT_BLACK);
  SCREEN.drawString(F("输出:"), 10, 57);
  SCREEN.drawFloat(store_out_kwh, 2, 52,58);
  SCREEN.drawString(F("KWh"), 120, 57);
  SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
  SCREEN.drawString(F("运行状态:"), 10, 78);
  // 重置按下事件
  ec11.add_Key_Listener(NULL, NULL, screen_page_200_longpress);
}

void screen_page_200_loop(){
  if(screen_after_1s()){
    SCREEN.fillRect(0, 99, 160, 30, TFT_BLACK);
    SCREEN.setTextColor(TFT_MAGENTA, TFT_BLACK);
    SCREEN.drawString(getSysInfo(), 10, 99);
  }
}

void screen_page_200_longpress(){
  store_sys_data(); //保存数据到EEPROM
  ESP.restart();
}


